package com.kujie.access.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kujie.access.domain.po.PropertyListManagement;
import com.kujie.access.domain.query.PropertyListQuery;
import com.kujie.access.domain.vo.PropertyListManageVO;
import com.kujie.access.service.IPropertyListService;
import com.kujie.common.core.domain.R;
import com.kujie.common.encrypt.annotation.ApiEncrypt;
import com.kujie.common.excel.utils.ExcelUtil;
import com.kujie.common.mybatis.core.page.PageQuery;
import com.kujie.common.web.core.BaseController;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 房源管理 api
 *
 * @author ck
 * @date 2025-05-26
 */
@Slf4j
@RestController
@RequestMapping("/property")
@RequiredArgsConstructor
@ApiEncrypt(response = true)
public class PropertyListController extends BaseController {
    private final IPropertyListService propertyListService;

    @GetMapping("/list")
    public R<Page<PropertyListManageVO>> list(PropertyListQuery propertyListQuery, PageQuery pageQuery) {
        return  R.ok(propertyListService.list(propertyListQuery, pageQuery));
    }

    @GetMapping("/detail")
    public R<PropertyListManageVO> detail(Long id) {
        return R.ok(propertyListService.detail(id));
    }

    @DeleteMapping("/delete/{ids}")
    public R<?> delete(@PathVariable List<Long> ids) {
        return toAjax(propertyListService.deleteByIds(ids));
    }

    @PostMapping("/insertOrUpdate")
    public R<?> add(@RequestBody PropertyListManagement propertyListManage) {
        return toAjax(propertyListService.insert(propertyListManage));
    }


    @PostMapping("/batchExport")
    public void batchExport(@RequestBody PropertyListQuery propertyListQuery, HttpServletResponse response) {
        List<PropertyListManageVO> propertyListManageVOS =  propertyListService.batchExport(propertyListQuery, response);
        ExcelUtil.exportExcel(propertyListManageVOS, "房源信息", PropertyListManageVO.class, response);
    }

    @PostMapping("/batchImport")
    public R<?> batchImport(@RequestPart("file") MultipartFile file) throws Exception {
        return propertyListService.batchImport(file);
    }

    @PostMapping("/exportTemplate")
    public void exportTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(CollectionUtil.newArrayList(), "房源信息导入模板", PropertyListManageVO.class, response);
    }
}
